/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trocabi.Socle;

/**
 *
 * @author philippe
 */
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.DecimalFormat;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

public class JNumberTextField extends JFormattedTextField implements KeyListener {
    private int nbDigit;

    public JNumberTextField(String format) {
        super();
        this.nbDigit = format.length();
        
        DecimalFormat decimalFormat = new DecimalFormat(format);
        decimalFormat.setMinimumIntegerDigits(nbDigit);
        decimalFormat.setMaximumFractionDigits(0);

        NumberFormatter textFormatter = new NumberFormatter(decimalFormat);
        textFormatter.setOverwriteMode(true);
        textFormatter.setAllowsInvalid(false);
        addKeyListener(this);
        NumberFormatter defaultFormatter = textFormatter;
        NumberFormatter displayFormatter = textFormatter;
        NumberFormatter editFormatter = textFormatter;
        // set their value classes
        defaultFormatter.setValueClass(Long.class);
        displayFormatter.setValueClass(Long.class);
        editFormatter.setValueClass(Long.class);
        // create and set the DefaultFormatterFactory
        DefaultFormatterFactory priceFactory = new DefaultFormatterFactory(defaultFormatter, displayFormatter, editFormatter);
        this.setFormatterFactory(priceFactory);
        setText("0");
    }

    public long getNumber() {
        return (Long) Long.parseLong(getText()==null?"0":getText());
    }

    public void setNumber(long value) {
        this.setText(Long.toString(value));
        this.setCaretPosition(0);
    }

    @Override
    public void keyTyped(KeyEvent ke) {
    }

    @Override
    public void keyPressed(KeyEvent ke) {
    }

    @Override
    public void keyReleased(KeyEvent ke) {
        String text = this.getText();
        if (text.length() > nbDigit) {
            super.setText(text.substring(0, text.length() - 1));
        }
    }

    public void setText(String value) {
        super.setText(value);
        this.setCaretPosition(0);
    }
    /**
     * Sample usage
     *
     * @param args
     * @throws ParseException
     */
    public static void main(final String args[]) throws ParseException {
        JFrame frame = new JFrame("Formatted Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JNumberTextField numberTextField = new JNumberTextField("####");
        //numberTextField.setNumber(1234);
        JTextField simpleText = new JTextField();
        frame.add(numberTextField, BorderLayout.NORTH);
        frame.add(simpleText, BorderLayout.SOUTH);
        frame.setSize(250, 100);
        frame.setVisible(true);
    }
}